package com.example.sso;

import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import sun.misc.BASE64Decoder;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;

public class TestSSO {

    public static void main(String[] args) {
        long l = System.currentTimeMillis();
        String id = "_jgBvkPG0EeeGBsnRn1YsAQ";
        String s = id + "_" + l + "__0000000000000000000000";
        String s1 = DigestUtil.md5Hex(s).toUpperCase();
        String format = String.format("http://10.111.14.140/login/portal/toLogon?check=%s&timestamp=%s&guid=%s", s1, l, id);
        System.out.println(format);
    }

    public String getLocation(String longitude, String latitude) {
        StringBuffer result = new StringBuffer();

        try {
            BufferedReader br = null;
            StringBuffer sb = new StringBuffer();
            URL url = new URL("http://api.map.baidu.com/ag/coord/convert?from=0&to=4&x=" + longitude + "&y=" + latitude);
            URLConnection conn = url.openConnection();
            br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
            String str = null;
            while ((str = br.readLine()) != null) {
                sb.append(str);
            }

            JSONObject fromObject = new JSONObject(sb.toString());
            BASE64Decoder decoder = new BASE64Decoder();
            //Base64.Decoder decoder = Base64.getDecoder();
            String lng = new String(decoder.decodeBuffer(fromObject.get("x").toString()), "utf-8");
            String lat = new String(decoder.decodeBuffer(fromObject.get("y").toString()), "utf-8");
            Map<String, String> mapLocation = new HashMap<String, String>();
		    /*map.put("lng", lng);
		    map.put("lat", lat);*/
            result.append("{\"lng\":\"").append(lng);
            result.append("\",\"lat\":\"").append(lat);
            result.append("\"}");
        } catch (IOException e) {

        }
        return result.toString();
    }

}
